package project.dade.cn.bst.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.api.GoogleApiClient;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.editorpage.ShareActivity;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2016/5/26.
 */
public class BrowserActivity extends ZJPBaseBarActivity implements View.OnClickListener{
    private static String TAG = BrowserActivity.class.getSimpleName();
    @InjectView(R.id.commonWebView)
    WebView mWebView;
    @InjectView(R.id.error_layout)
    EmptyLayout mErrorLayout;
    @InjectView(R.id.universal_toolBarTitle)
    TextView Title;
    @InjectView(R.id.universal_toolBar)
    TextView share;
    private int key = 0;
    WebSettings webSettings;
    private String mUrl;
    public static final int SHAREAPP = 0;// 分享APP
    public static final int USERLICENSING = 1; // 用户条例
    public static final int PRODUCTDETAIL = 2;//商品详情

    UMImage image = new UMImage(BrowserActivity.this, "http://www.umeng.com/images/pic/social/integrated_3.png");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        key = getIntent().getIntExtra("key", 0);
        initSetListener();
        share.setOnClickListener(this);
        initData();
    }

    private void initSetListener() {

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.removeAllViews();
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
               mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);// 完成

            }
        });

    }

    private void initData() {
        switch (key) {

            case SHAREAPP:
                // 分享APP
                Title.setText(getResources().getString(R.string.shareapp));
                mUrl = "http://114.55.55.209/ddshop/UserLicensing/shareapp.html";
                mWebView.loadUrl(mUrl);
                break;
            case USERLICENSING:
                Title.setText("用户协议");
                mUrl = "http://114.55.55.209/ddshop/UserLicensing/user_licensing.html";
                mWebView.loadUrl(mUrl);
                share.setVisibility(View.GONE);
                break;
            case  PRODUCTDETAIL:
              // 产品图文详情
                if (!TextUtils.isEmpty(getIntent().getStringExtra("url"))) {
                   Title.setText("商品详情");
                    webSettings.setUseWideViewPort(true);
                    webSettings.setLoadWithOverviewMode(true);
                    webSettings.setBuiltInZoomControls(true);
                    // 设置WebView的缓存
                    if (AppUtil.isNetworkAvailable(this)) {
                        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
                    } else {
                        webSettings
                                .setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                    }
                    // 显示内容
                    mUrl = getIntent().getStringExtra("url");
                    mWebView.loadUrl(mUrl);
                    share.setVisibility(View.GONE);
                } else {
                    share.setVisibility(View.GONE);
                    mErrorLayout.setErrorType(EmptyLayout.NODATA);// 无数据
                    mErrorLayout.setErrorMessage("无图文详情");
                }
                break;


        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.universal_toolBar:
                new ShareAction(this).setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                        .withTitle("来自一个好用的APP")
                        .withText("速惠是一个很好用的，下载链接 http://114.55.55.209/ddshop/UserLicensing/shareapp.html")
                        .withTargetUrl("'www.baidu.com")
                        .withMedia(image)
                        .setCallback(umShareListener)
                        .open();
                break;
        }

    }
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat","platform"+platform);
            if(platform.name().equals("WEIXIN_FAVORITE")){
                Toast.makeText(BrowserActivity.this,platform + " 收藏成功啦",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(BrowserActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(BrowserActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(BrowserActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
